/**
 * Created by Zero on 2017/12/5.
 */
//用户登录
var logInCtrl=angular.module('logInCtrl',[]);
logInCtrl.controller("logIn",function ($scope,$http) {

    $scope.user = {
        username:null,
        password:null
    };
    $scope.logIn = function () {
        $http({
            method:"post",
            url:"http://localhost:8080/user/login",
            data: $scope.user
        }).success(function (data) {
            switch (data){
                case 1:
                    jSuccess("用户登录成功",{
                        VerticalPosition : 'center',
                        HorizontalPosition : 'center'
                    });
                    window.setTimeout("window.location.href='index.html'",1000);
                    break;
                case 2:
                    jError("该账户已被锁定，请联系管理员处理!!",{
                        VerticalPosition : 'center',
                        HorizontalPosition : 'center'
                    });
                    break;
                case 0:
                    console.log(data);
                    jError("用户信息错误!!",{
                        VerticalPosition : 'center',
                        HorizontalPosition : 'center'
                    });
                    break;
            }
        }).error(function(){
            console.log("fail");
            jError("登录失败，请重试!!",{
                VerticalPosition : 'center',
                HorizontalPosition : 'center'
            });
        })
    }
});


//注册用户
var addUserCtrl=angular.module('addUserCtrl',[]);
addUserCtrl.controller("addUser",function ($scope,$http) {

    $scope.user = {
        username:null,
        password:null,
        name:null,
        phone:null
    };
    $scope.addUser = function () {
        $http({
            method:"post",
            url:"http://localhost:8080/user/add",
            data: $scope.user
        }).success(function (data) {
            switch (data){
                case 1:
                    jSuccess("用户注册成功",{
                        VerticalPosition : 'center',
                        HorizontalPosition : 'center'
                    });
                    window.setTimeout("window.location.href='login.html'",1000);
                    break;
                case 0:
                    console.log(data);
                    jError("注册失败(用户名已存在)!!",{
                        VerticalPosition : 'center',
                        HorizontalPosition : 'center'
                    });
                    break;
            }
        }).error(function(){
            console.log("fail");
            jError("系统错误，请重试!!",{
                VerticalPosition : 'center',
                HorizontalPosition : 'center'
            });
        })
    }
});


//添加留言
var indexCtrl=angular.module('indexCtrl',[]);
indexCtrl.controller("index",function ($scope,$http) {

    $scope.idea = {
        content:null
    };
    $scope.addIdea = function () {
        $http({
            method:"post",
            url:"http://localhost:8080/idea/add",
            data: $scope.idea
        }).success(function (data) {
            switch (data){
                case 1:
                    jSuccess("添加留言成功",{
                        VerticalPosition : 'center',
                        HorizontalPosition : 'center'
                    });
                    window.setTimeout("window.location.href='index.html'",1000);
                    break;
                case 0:
                    console.log(data);
                    jError("请先登录!!",{
                        VerticalPosition : 'center',
                        HorizontalPosition : 'center'
                    });
                    window.setTimeout("window.location.href='login.html'",1000);
                    break;
            }
        }).error(function(){
            console.log("fail");
            jError("系统错误，请重试!!",{
                VerticalPosition : 'center',
                HorizontalPosition : 'center'
            });
        })
    }
});


//发布游记
var addTravelsCtrl=angular.module('addTravelsCtrl',[]);
addTravelsCtrl.controller("addTravels",function ($scope,$http) {

    $scope.travels = {
        title:null,
        content:null,
        picture:null
    };
    $scope.addTravels = function () {
        $http({
            method:"post",
            url:"http://localhost:8080/travels/add",
            data: $scope.travels
        }).success(function (data) {
            if(data!=0){
                var fd = new FormData();
                var file = document.getElementById("file-1").files[0];
                console.log(file);
                fd.append("file", file);
                // 上传活动
                $http({
                    method: "post",
                    url: "http://localhost:8080/picture/travels/"+data,
                    data: fd,
                    headers: {'Content-Type': undefined}
                }).success(function (data) {
                    //处理成功
                    console.log(data+"成功");
                }).error(function (data) {
                    //处理失败
                    console.log(data)
                });
                jSuccess("发布游记成功",{
                    VerticalPosition : 'center',
                    HorizontalPosition : 'center'
                });
                window.setTimeout("window.location.href='index.html'",1000);
            }else{
                jError("请先登录!!",{
                    VerticalPosition : 'center',
                    HorizontalPosition : 'center'
                });
                window.setTimeout("window.location.href='login.html'",1000);
            }
        }).error(function(){
            console.log("fail");
            jError("系统错误，请重试!!",{
                VerticalPosition : 'center',
                HorizontalPosition : 'center'
            });
        });

    }
});


//游记展示
var showTravelsCtrl=angular.module('showTravelsCtrl',[]);
showTravelsCtrl.controller("showTravels",function ($scope,$http) {
    $scope.addTravels = function (count) {
        $http({
            method:"get",
            url:"http://localhost:8080/travels/show/"+count
        }).success(function (data) {
            console.log(data.travelses);
            $scope.travelses=data.travelses;
            if($scope.travelses==null){
                jError("游记记录加载完...",{
                    VerticalPosition : 'center',
                    HorizontalPosition : 'center'
                });
            }
        }).error(function(){
            console.log("fail");
            jError("系统错误，请重试!!",{
                VerticalPosition : 'center',
                HorizontalPosition : 'center'
            });
        })
    };
    $scope.addTravels(0);
});

//个人游记展示
var showPersonalTravelsCtrl=angular.module('showPersonalTravelsCtrl',[]);
showPersonalTravelsCtrl.controller("showPersonalTravels",function ($scope,$http) {

    function GetRequest() {
        var url = location.search; //获取url中"?"符后的字串
        if (url.indexOf("?") != -1) {    //判断是否有参数
            var str = url.substr(1); //从第一个字符开始 因为第0个是?号 获取所有除问号的所有符串
            strs = str.split("=");   //用等号进行分隔 （因为知道只有一个参数 所以直接用等号进分隔 如果有多个参数 要用&号分隔 再用等号进行分隔）
            return strs[1];          //直接弹出第一个参数 （如果有多个参数 还要进行循环的）
        }
    }
    $scope.showPersonalTravels = function (uId) {
        $http({
            method:"get",
            url:"http://localhost:8080/travels/personal/"+uId
        }).success(function (data) {
            console.log(data.travelses);
            $scope.travelses=data.travelses;
            $scope.userName=$scope.travelses[0].userName;
        }).error(function(){
            console.log("fail");
            jError("系统错误，请重试!!",{
                VerticalPosition : 'center',
                HorizontalPosition : 'center'
            });
        })
    };
    $scope.showPersonalTravels(GetRequest());
});


//新闻展示
var newsShowCtrl=angular.module('newsShowCtrl',[]);
newsShowCtrl.controller("newsShow",function ($scope,$http) {


    var showPic = function (){
        var imgDiv = document.getElementById("imgDiv");
        var divImg = imgDiv.getElementsByTagName("img");
        var xinwen_table = document.getElementById("xinwen_table");
        var tabletr = xinwen_table.getElementsByTagName("tr");
        for(var i = 0; i < tabletr.length; i++) {
            tabletr[i].index = i;
            tabletr[i].onmouseover = function() {
                for(var j = 0; j < divImg.length; j++) {
                    for(var j = 0; j < divImg.length; j++) {
                        divImg[j].style.display = "none";
                    }
                    divImg[this.index].style.display = "block";
                }
            }
        }
    };

    $scope.changeNews=function(){
        window.location.href="yajiang_xinwendetail.html?state=1";
    };

    $scope.changeNotice=function(){
        window.location.href="yajiang_xinwendetail.html?state=2";
    };

    $http({
        method:"get",
        url:"http://localhost:8080/news/findNotice"
    }).success(function (data) {
        $scope.notices=data.notices;
    }).error(function(){
        console.log("fail");
        jError("系统错误，请重试!!",{
            VerticalPosition : 'center',
            HorizontalPosition : 'center'
        });
    });

    $http({
        method:"get",
        url:"http://localhost:8080/news/findNews"
    }).success(function (data) {
        $scope.newses=data.newses;
    }).error(function(){
        console.log("fail");
        jError("系统错误，请重试!!",{
            VerticalPosition : 'center',
            HorizontalPosition : 'center'
        });
    });

    showPic();
});



//活动展示
var activityShowCtrl=angular.module('activityShowCtrl',[]);
activityShowCtrl.controller("activityShow",function ($scope,$http) {

    $http({
        method:"get",
        url:"http://localhost:8080/activity/findAll"
    }).success(function (data) {
        $scope.activities=data.activities;
    }).error(function(){
        console.log("fail");
        jError("系统错误，请重试!!",{
            VerticalPosition : 'center',
            HorizontalPosition : 'center'
        });
    });
});



//详细新闻展示
var AllNewsShowCtrl=angular.module('AllNewsShowCtrl',[]);
AllNewsShowCtrl.controller("AllNewsShow",function ($scope,$http) {

    function GetRequest() {
        var url = location.search; //获取url中"?"符后的字串
        if (url.indexOf("?") != -1) {    //判断是否有参数
            var str = url.substr(1); //从第一个字符开始 因为第0个是?号 获取所有除问号的所有符串
            strs = str.split("=");   //用等号进行分隔 （因为知道只有一个参数 所以直接用等号进分隔 如果有多个参数 要用&号分隔 再用等号进行分隔）
            return strs[1];          //直接弹出第一个参数 （如果有多个参数 还要进行循环的）
        }
    }
    if(GetRequest()==1){
        $scope.name="新闻";
        $http({
            method:"get",
            url:"http://localhost:8080/news/findNews"
        }).success(function (data) {
            $scope.newses=data.newses;
        }).error(function(){
            console.log("fail");
            jError("系统错误，请重试!!",{
                VerticalPosition : 'center',
                HorizontalPosition : 'center'
            });
        });
    }else if(GetRequest()==2){
        $scope.name="公告";
        $http({
            method:"get",
            url:"http://localhost:8080/news/findNotice"
        }).success(function (data) {
            $scope.newses=data.notices;
        }).error(function(){
            console.log("fail");
            jError("系统错误，请重试!!",{
                VerticalPosition : 'center',
                HorizontalPosition : 'center'
            });
        });
    }
});


//留言展示
var ideaShowCtrl=angular.module('ideaShowCtrl',[]);
ideaShowCtrl.controller("ideaShow",function ($scope,$http) {

    $http({
        method:"get",
        url:"http://localhost:8080/idea/show"
    }).success(function (data) {
        $scope.ideas=data.ideas;
    }).error(function(){
        console.log("fail");
        jError("系统错误，请重试!!",{
            VerticalPosition : 'center',
            HorizontalPosition : 'center'
        });
    });
});


//添加新闻和公告
var addNewsCtrl=angular.module('addNewsCtrl',[]);
addNewsCtrl.controller("addNews",function ($scope,$http) {

    $scope.news1 = {
        title:null,
        content:null
    };
    $scope.news2 = {
        title:null,
        content:null
    };
    $scope.addNews1 = function () {
        $http({
            method:"post",
            url:"http://localhost:8080/news/addNews",
            data: $scope.news1
        }).success(function (data) {
            if(data!=0){
                var fd = new FormData();
                var file = document.getElementById("news").files[0];
                console.log(file);
                fd.append("file", file);
                // 上传活动
                $http({
                    method: "post",
                    url: "http://localhost:8080/picture/news/"+data,
                    data: fd,
                    headers: {'Content-Type': undefined}
                }).success(function (data) {
                    //处理成功
                    console.log(data+"成功");
                }).error(function (data) {
                    //处理失败
                    console.log(data)
                });
                jSuccess("添加新闻动态成功",{
                    VerticalPosition : 'center',
                    HorizontalPosition : 'center'
                });
            }else{
                console.log(data);
                jError("添加失败!!",{
                    VerticalPosition : 'center',
                    HorizontalPosition : 'center'
                });
            }
        }).error(function(){
            console.log("fail");
            jError("系统错误，请重试!!",{
                VerticalPosition : 'center',
                HorizontalPosition : 'center'
            });
        })
    };

    $scope.addNotice = function () {
        $http({
            method:"post",
            url:"http://localhost:8080/news/addNotice",
            data: $scope.news2
        }).success(function (data) {
            if(data!=0){
                var fd = new FormData();
                var file = document.getElementById("notice").files[0];
                console.log(file);
                fd.append("file", file);
                // 上传活动
                $http({
                    method: "post",
                    url: "http://localhost:8080/picture/notice/"+data,
                    data: fd,
                    headers: {'Content-Type': undefined}
                }).success(function (data) {
                    //处理成功
                    console.log(data+"成功");
                }).error(function (data) {
                    //处理失败
                    console.log(data)
                });
                jSuccess("添加公告成功",{
                    VerticalPosition : 'center',
                    HorizontalPosition : 'center'
                });
            }else{
                console.log(data);
                jError("添加失败!!",{
                    VerticalPosition : 'center',
                    HorizontalPosition : 'center'
                });
            }
        }).error(function(){
            console.log("fail");
            jError("系统错误，请重试!!",{
                VerticalPosition : 'center',
                HorizontalPosition : 'center'
            });
        })
    };
});